package com.ruoyi.work.service.impl;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.BmInorderlistMapper;
import com.ruoyi.work.domain.BmInorderlist;
import com.ruoyi.work.service.IBmInorderlistService;

/**
 * 订单采购统计Service业务层处理
 * 
 * @author sj
 * @date 2023-08-03
 */
@Service
public class BmInorderlistServiceImpl implements IBmInorderlistService 
{
    @Autowired
    private BmInorderlistMapper bmInorderlistMapper;

//    实现按年统计采购中的数量和金额
    @Override
    public List<Map<String, Object>> getCount(BmInorderlist bmInorderlist) {
        System.out.println(bmInorderlist);
        if (bmInorderlist.getMechanismName()!=null && bmInorderlist.getMechanismName().length() >0){
//            拿到仓库管理人员的id集合
            Integer[] idList = bmInorderlistMapper.getEmployeeId(bmInorderlist.getMechanismName());
            bmInorderlist.setIdList(idList);
        }
        if (bmInorderlist.getBusinessName()!=null && bmInorderlist.getBusinessName().length() >0){
//            拿到商家名称对应的id
            Integer businessId = bmInorderlistMapper.getBusinessId(bmInorderlist.getBusinessName());
            bmInorderlist.setSellerId(businessId.longValue());
        }
//        拿到仓库编码对应的仓库管理人员id集合
        if (bmInorderlist.getWarehouserId() != null && bmInorderlist.getWarehouserId() >0){
            Integer[] codeList = bmInorderlistMapper.getWarehouseCode(bmInorderlist.getWarehouseCode());
            bmInorderlist.setCodeList(codeList);
        }
        if (bmInorderlist.getRadio() != null && bmInorderlist.getRadio().intValue() == 1
                    && bmInorderlist.getStartDate() != null && bmInorderlist.getEndDate()  != null){
            List<Map<String,Object>> result1 = bmInorderlistMapper.getCountYear1(bmInorderlist);
            List<Map<String,Object>> result2 = bmInorderlistMapper.getCountYear2(bmInorderlist);
            List<Map<String,Object>> result = new ArrayList<>();
            BigDecimal totalPrice = new BigDecimal(0.00);
            for (int i = 0; i <result1.size(); i++) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",result1.get(i).get("date"));
                map.put("sumNum1",result1.get(i).get("sumNum1"));
                map.put("sumNum2",result2.get(i).get("sumNum2"));
                BigDecimal p1 = (BigDecimal) result1.get(i).get("sumPrice");
                BigDecimal p2 = (BigDecimal) result2.get(i).get("sumPrice");
                totalPrice = totalPrice.add(p1.add(p2));
                if ( i == result1.size()-1 ){
                    map.put("totalPrice",totalPrice);
                    result.add(map);
                }else {
                    result.add(map);
                }
            }
            return result;
        }else if (bmInorderlist.getRadio() != null && bmInorderlist.getRadio().intValue() == 2
                    && bmInorderlist.getStartDate() != null && bmInorderlist.getEndDate()  != null ){
            List<Map<String,Object>> result1 = bmInorderlistMapper.getCountMonth1(bmInorderlist);
            List<Map<String,Object>> result2 = bmInorderlistMapper.getCountMonth2(bmInorderlist);
            List<Map<String,Object>> result = new ArrayList<>();
            BigDecimal totalPrice = new BigDecimal(0.00);
            for (int i = 0; i <result1.size(); i++) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",result1.get(i).get("date"));
                map.put("sumNum1",result1.get(i).get("sumNum1"));
                map.put("sumNum2",result2.get(i).get("sumNum2"));
                BigDecimal p1 = (BigDecimal) result1.get(i).get("sumPrice");
                BigDecimal p2 = (BigDecimal) result2.get(i).get("sumPrice");
                totalPrice = totalPrice.add(p1.add(p2));
                if ( i == result1.size()-1 ){
                    map.put("totalPrice",totalPrice);
                    result.add(map);
                }else {
                    result.add(map);
                }
            }
            return result;
        }else if (bmInorderlist.getRadio() != null && bmInorderlist.getRadio().intValue() == 3
                    && bmInorderlist.getStartDate() != null && bmInorderlist.getEndDate()  != null ){
            List<Map<String,Object>> result1 = bmInorderlistMapper.getCountDay1(bmInorderlist);
            List<Map<String,Object>> result2 = bmInorderlistMapper.getCountDay2(bmInorderlist);
            List<Map<String,Object>> result = new ArrayList<>();
            BigDecimal totalPrice = new BigDecimal(0.00);
            for (int i = 0; i <result1.size(); i++) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",result1.get(i).get("date"));
                map.put("sumNum1",result1.get(i).get("sumNum1"));
                map.put("sumNum2",result2.get(i).get("sumNum2"));
                BigDecimal p1 = (BigDecimal) result1.get(i).get("sumPrice");
                BigDecimal p2 = (BigDecimal) result2.get(i).get("sumPrice");
                totalPrice = totalPrice.add(p1.add(p2));
                if ( i == result1.size()-1 ){
                    map.put("totalPrice",totalPrice);
                    result.add(map);
                }else {
                    result.add(map);
                }
            }
            return result;
        }else {
            throw new RuntimeException("请选择按年/月/日统计订单中的数量和总金额 且 指定查询的日期范围");
        }
    }
    @Override
    public List<Map<String, Object>> getWarehouseName() {
        return bmInorderlistMapper.getWarehouseName();
    }
}
